package 刷题.leedcode226;

import java.util.LinkedList;
import java.util.Queue;

//层序遍历，使用队列
public class Solution2 {
    public TreeNode invertTree(TreeNode root){
        if(root == null){
            return null;
        }
        Queue<TreeNode> stack = new LinkedList<>();
        stack.offer(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.poll();
            //保存右子树结点
            TreeNode rightTree = node.right;
            node.right = node.left;
            node.left = rightTree;
            if(node.left != null){
                stack.offer(node.left);
            }
            if(node.right != null){
                stack.offer(node.right);
            }
        }
        return root;
    }
}
